<?php
class Application_Model_StockInPortfolio
{
	protected $_dbTable;
	public $_mId;
	public $_mStockId;
	public $_mStockCode;
	public $_mVolume;
	public $_mNetExpense;
	public $_mPortfolioId;

	public function getId(){
		return $this->_mId;
	}
	public function setId($value){
		$this->_mId = $value;
		return $this;
	}
	public function getStockId(){
		return $this->_mStockId;
	}
	public function setStockId($value){
		$this->_mStockId = $value;
		return $this;
	}
	public function getStockCode(){
		return $this->_mStockCode;
	}
	public function setStockCode($value){
		$this->_mStockCode = $value;
		return $this;
	}
	public function getVolume(){
		return $this->_mVolume;
	}
	public function setVolume($value){
		$this->_mVolume = $value;
		return $this;
	}
	public function getNetExpense(){
		return $this->_mNetExpense;
	}
	public function setNetExpense($value){
		$this->_mNetExpense = $value;
		return $this;
	}
	public function getPortfolioId(){
		return $this->_mPortfolioId;
	}
	public function setPortfolioId($value){
		$this->_mPortfolioId = $value;
		return $this;
	}


	public function setDbTable($dbTable){
		if (is_string($dbTable))
			$dbTable = new $dbTable();
		if (!$dbTable instanceof Zend_Db_Table_Abstract)
			throw new Exception('Invalid table data gateway provided');
		$this->_dbTable = $dbTable;
		return $this;
	}
	public function getDbTable(){
		if (null === $this->_dbTable)
			$this->setDbTable('Application_Model_DbTable_StockInPortfolio');
		return $this->_dbTable;
	}
	public function save(Application_Model_StockInPortfolio $old = null){
		$item = $this;
		$data = array();
		if(isset($item->_mId)) $data['id'] = $item->_mId;
		if(isset($item->_mStockId)) $data['stock_id'] = $item->_mStockId;
		if(isset($item->_mStockCode)) $data['stock_code'] = $item->_mStockCode;
		if(isset($item->_mVolume)) $data['volume'] = $item->_mVolume;
		if(isset($item->_mNetExpense)) $data['net_expense'] = $item->_mNetExpense;
		if(isset($item->_mPortfolioId)) $data['portfolio_id'] = $item->_mPortfolioId;

		if($old === null)
			$this->getDbTable()->insert($data);
		else{
			$key = array();
			$old->_mId === null ? $key[] = 'id is NULL' : $key['id = ?'] =  $old->_mId;

			$this->getDbTable()->update($data, $key);
		}
	}
	public function delete(){
		$item = $this;
		$old = $item;
		$key = array();
			$old->_mId === null ? $key[] = 'id is NULL' : $key['id = ?'] =  $old->_mId;

		$this->getDbTable()->delete($key);
	}
	public function fetchAll($where = null, $order = null, $count = null, $offset = null){
		$resultSet = $this->getDbTable()->fetchAll($where, $order, $count, $offset);
		$entries   = array();
		foreach ($resultSet as $row) {
			$item = new Application_Model_StockInPortfolio();
			if(isset($row->id))$item->_mId = $row->id;
			if(isset($row->stock_id))$item->_mStockId = $row->stock_id;
			if(isset($row->stock_code))$item->_mStockCode = $row->stock_code;
			if(isset($row->volume))$item->_mVolume = $row->volume;
			if(isset($row->net_expense))$item->_mNetExpense = $row->net_expense;
			if(isset($row->portfolio_id))$item->_mPortfolioId = $row->portfolio_id;

			$entries[] = $item;
		}
		return $entries;
	}
}
